# coding=utf-8
from django.shortcuts import render
from .methods.labelMethods import get_img_paths, get_unlabel_img_count
from .actions.loginAction import checkSession
from .actions.classifyAction import get_models_cfg

# 跳转到登录界面
def to_login_page(request):
    # 先检查是否登录过
    data ={}
    user = checkSession(request)
    # 如果已经登录过，就无需再登录
    if user != None:
        data['message'] = '您已经登录了……'
        data['url'] = '/classifyPage'
        data['action'] = '返回分类界面'
        return render(request, 'result.html', data)
    # 如果未登录就跳转到登录界面
    else:
        return render(request, 'login.html')
    
# 跳转到分类界面
def to_classify_page(request):
    img_url = None
    if request.POST:
        img_url = request.POST.get('img_url')
    else:
        img_url = request.GET.get('img_url')
    data = {}
    data['img_url'] = img_url
    data['username'] = None
    # 检查是否登录
    user = checkSession(request)
    if user != None:
        data['username'] = user['username']
    print(data)
    return render(request, 'classify.html' , data)

# 跳转到标注界面
def to_label_page(request):
    data = {}
    data['username'] = None
    # 检查是否登录
    user = checkSession(request)
    if user != None:
        # 只有标注员和管理员可以进入这个页面
        if user['rank'] == 'marker' or user['rank'] == 'admin':
            img_paths = get_img_paths()
            count = get_unlabel_img_count()
            data['img_path_List'] = img_paths
            data['unlabel_img_count'] = count
            data['username'] = user['username']
            return render(request, 'label.html', data)
        else:
            data['message'] = '您没有标注的权限……'
            data['url'] = '/classifyPage'
            data['action'] = '返回分类界面'
            return render(request, 'result.html', data)
    # 如果未登录就跳转到登录界面
    else:
        data['message'] = '您还没有登录……'
        data['url'] = '/loginPage'
        data['action'] = '去登录'
        return render(request, 'result.html', data)

# 跳转到爬虫界面
def to_crawler_page(request):
    data = {}
    data['username'] = None
    # 先检查是否登录过
    user = checkSession(request)
    if user != None:
        # 只有标注员和管理员可以进入这个页面
        if user['rank'] == 'marker' or user['rank'] == 'admin':
            data['username'] = user['username']
            return render(request, 'crawler.html', data)
        else:
            data['message'] = '您没有运行爬虫程序的权限……'
            data['url'] = '/classifyPage'
            data['action'] = '返回分类界面'
            return render(request, 'result.html', data)
    # 如果未登录就跳转到登录界面
    else:
        data['message'] = '您还没有登录……'
        data['url'] = '/loginPage'
        data['action'] = '去登录'
        return render(request, 'result.html', data)

# 跳转到算法调参页面
def to_algorithm_page(request):
    data = {}
    data['username'] = None
    # 先检查是否登录过
    user = checkSession(request)
    if user != None:
        # 只有算法工程师与管理员可以进入这个页面
        if user['rank'] == 'algorithm' or user['rank'] == 'admin':
            clf_type = request.session.get('clf_type', 'SVM')
            data['clf_type'] = clf_type
            data['username'] = user['username']
            # 获取之前的模型配置
            svm_result, knn_result, rf_result = get_models_cfg()
            data['svm'] = svm_result
            data['knn'] = knn_result
            data['rf'] = rf_result
            return render(request, 'algorithm.html',data)
        else:
            data['message'] = '您没有修改算法的权限……'
            data['url'] = '/classifyPage'
            data['action'] = '返回分类界面'
            return render(request, 'result.html', data)
    # 如果未登录就跳转到登录界面
    else:
        data['message'] = '您还没有登录……'
        data['url'] = '/loginPage'
        data['action'] = '去登录'
        return render(request, 'result.html', data)
